package com.tjhk.taos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjhk.taos.entity.Location;
import com.tjhk.taos.mapper.LocationMapper;
import com.tjhk.taos.service.LocationService;
import com.tjhk.taos.util.BeanUtil;
import com.tjhk.taos.util.STableUtil;
import com.tjhk.taos.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class LocationServiceImpl implements LocationService {

    @Resource
    private LocationMapper mapper;

    @Override
    public int insert(Location one) {
        if (one.getPlateNo().length() != 7 ){
            log.warn("车牌信息异常:{}",one.getPlateNo());
            return 0;
        }
        if (!StringUtil.isContainChinese(one.getPlateNo())){
            log.warn("车牌信息异常:{}",one.getPlateNo());
            return 0;
        }
        if (StringUtil.isContainChinese(one.getPlateNo().substring(1))){
            log.warn("车牌信息异常:{}",one.getPlateNo());
            return 0;
        }
        Map<String,Object> map= BeanUtil.fromJavaBean(one);
        map.put("tableName",STableUtil.getTableName(one.getPlateNo()));
        int affectRows = mapper.insertOne(map);
        return affectRows;
    }

    @Override
    public int batchInsert(List<Location> list) {
        Map<String,List<Location>> map= list.stream().collect(Collectors.groupingBy(Location::getPlateNo));
         map.forEach((k,v)->{
            String tableName = STableUtil.getTableName(k);
            mapper.batchInsert(tableName,k,v);
        });
        return list.size();
    }

    @Override
    public List<Location> list(String plateNo) {
        return mapper.selectList(new LambdaQueryWrapper<Location>().eq(Location::getPlateNo,plateNo));
    }

    @Override
    public List<Location> list(String plateNo, LocalDateTime st, LocalDateTime end) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isBlank(plateNo)){
            queryWrapper.last("limit 1000");
        }else {
            queryWrapper.eq(StringUtils.isNotBlank(plateNo),Location::getPlateNo,plateNo);
        }
        if (st != null){
            queryWrapper.gt(Location::getSt, Timestamp.valueOf(st).toString());
        }
        if (end != null){
            queryWrapper.lt(Location::getSt, Timestamp.valueOf(end).toString());
        }
        return mapper.selectList(queryWrapper);
    }

    @Override
    public IPage<Location> page(int current, int size, String plateNo, LocalDateTime st, LocalDateTime end) {
        IPage page = new Page(current, size);
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isBlank(plateNo)){
            queryWrapper.last("limit 1000");
        }else {
            queryWrapper.eq(StringUtils.isNotBlank(plateNo),Location::getPlateNo,plateNo);
        }
        if (st != null){
            queryWrapper.gt(Location::getSt, Timestamp.valueOf(st).toString());
        }
        if (end != null){
            queryWrapper.lt(Location::getSt, Timestamp.valueOf(end).toString());
        }
        IPage<Location> locationIPage = mapper.selectPage(page,queryWrapper);
        return locationIPage;
    }
}
